from typing import Iterable, List
import filetype

from langchain_core.documents import Document
from config.core.aigc import *
def get_file_type_from_url(url: str):
    """Determine the file type from the URL using the filetype package."""
    kind = filetype.guess(url)
    if kind is None:
        return None
    return kind.extension  # Returns the file extension
    
def unify_documents_meta(documents: Iterable[Document], file_type: str = None, title: str = "", source_url:str=None) -> List[Document]:
    """Besides page_content, add following metadata to documents: page, source, title, description, language, file_type

    Args:
        documents (Iterable[Document]): Document List

    Returns:
        List[Document]: Document List with unified metadata
    """    
    docs = []
    for doc in documents:
        if doc.metadata.get('file_type') is None:
            doc.metadata['file_type'] = file_type
        
        # TODO: retrieve title, description, language for pdf file
        if doc.metadata.get('file_type') == FILE_TYPE_PDF:
            doc.metadata['title'] = title
            doc.metadata['description'] = ""
            doc.metadata['language'] = ""
        elif doc.metadata.get('file_type') == FILE_TYPE_HTML:
            doc.metadata['page'] = 0
            if not doc.metadata.get('title') and title != "":
                doc.metadata['title'] = title 
        elif doc.metadata.get('file_type') == FILE_TYPE_AUDIO or doc.metadata.get('file_type') == FILE_TYPE_IMAGE:
            doc.metadata['page'] = 0
            doc.metadata['title'] = title
            doc.metadata['description'] = ""   
            doc.metadata['language'] = ""            
        else:
            doc.metadata['page'] = 0
            doc.metadata['title'] = title
            doc.metadata['description'] = ""
            doc.metadata['language'] = ""
        if doc.metadata.get('source_url') is None:
            doc.metadata['source_url'] = source_url
        docs.append(doc)
        
    return docs

def get_document_content_from_file(pages: list[Document]) -> str:
    """Determine the file type from the URL using the filetype package."""
    content = ""
    for page in pages:
        if len(content) < 2000:
            content += page.page_content + "\n"
        else:
            break
        # content += page.page_content + "\n"
    return content  # Returns the file extension